package ajakuulauku;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import javax.swing.Icon;
import javax.swing.ImageIcon;

public class GameObject {
	/**
	 * The Object's locations in coordinates
	 */
	private double x;
	private double y;
	public boolean isMovable;
	public boolean isCollideable;
	String iconName;
	
	/**
	 * The Object's radius
	 */
	private double radius;
	
	/**
	 * The image which is to be drawn
	 */
	private BufferedImage image;
	
	/**
	 * Holds original location of object
	 */
	private double originalX, originalY;
	
	public void Update() {
		
	}
	
	/**
	 * Draws the Object
	 * @param g, Graphics2D
	 */
	public void draw(Graphics2D g) {
		g.drawImage(image, null, (int)(Math.round(this.x) - this.radius), (int)(Math.round(this.y) - this.radius));
	}
	
	public void parseParam(String name, String value) {
		if(name == "locationX") {
			setX(Double.parseDouble(value));
		}else if(name == "locationY") {
			setY(Double.parseDouble(value));
		}
	}
	
	public void finishCreating() {
		
	}
	
	public Icon getEditorIcon() {
		return GUI.loadIcon(iconName);
	}
	
	/**
	 * Checks if the GameObject is colliding with the other GameObject
	 * @param other, gameObject
	 * @return True if it collided, else False
	 */
	public boolean isColliding (GameObject other) {
		boolean collided = false;
		double distance = new Vector2(x-other.x, y-other.y).length();
		if (distance < (radius + other.radius + 2)) // 2 pixels added for a little buffer
			collided = true;
		
		return collided;
	}
	
	/**
	 * Checks if the Ball is clicked on
	 * @param clickX, coordinate
	 * @param clickY, coordinate
	 * @return True if it was clicked on, else False
	 */
	public boolean clickedOn (int clickX, int clickY) {
		boolean clicked = false;
		Vector2 magnet = new Vector2(this);
		Vector2 click = new Vector2(clickX, clickY);
		if (magnet.subtract(click).length() < radius)
			clicked = true;
		
		return clicked;
	}
	
	/**
	 * The point from which the GameObject was dragged from
	 * @param x, coordinate
	 * @param y, coordinate
	 */
	public void startDrag(int x, int y) {
		originalX = getX();
		originalY = getY();
		setX(x);
		setY(y);
	}
	
	/**
	 * The place where the ball is during the drag
	 * @param x, coordinate
	 * @param y
	 */
	public void drag(int x, int y) {
		setX(x);
		setY(y);
	}

	/**
	 * The place where the drag ended
	 * @param x, coordinate
	 * @param y, coordinate
	 */
	public void endDrag(int x, int y) {
		setX(x);
		setY(y);
	}
	
	public void cancelDrag() {
		setX(originalX);
		setY(originalY);
	}
	
	/**
	 * Sets the GameObject's image to the one specified in the string
	 * @param i, String("images/*.*);
	 */
	public void setImage(String i) {
		iconName = i;
		image = GUI.loadBufferedImage(i);
	}

	/**
	 * Sets the GameObject's X coordinate to the one specified
	 * @param x, coordinate
	 */
	public void setX(double x) {
		this.x = x;
	}

	/**
	 * Returns the GameObject's X coordinate
	 * @return
	 */
	public double getX() {
		return x;
	}

	/**
	 * Sets the GameObject's Y coordinate to the one specified
	 * @param y, coordinate
	 */
	public void setY(double y) {
		this.y = y;
	}

	/**
	 * Returns the GameObject's Y coordinate
	 * @return
	 */
	public double getY() {
		return y;
	}

	/**
	 * Sets the GameObject's radius to the one specified
	 * @param radius, double, new radius
	 */
	public void setRadius(double radius) {
		this.radius = radius;
	}

	public double getRadius() {
		return radius;
	}
	
	public XMLElement getXMLElement() {
		XMLElement element = new XMLElement(this.getClass().getSimpleName().toLowerCase());
		element.addParam("locationX", Double.toString(getX()));
		element.addParam("locationY", Double.toString(getY()));
		return element;
	}
}
